﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rapid
{
    public partial class frmRapidRead : frmMenustrip
    {
        public const bool VERTICAL = true;
        public const bool HORIZONTAL = false;

        public bool _currentDirection;
        
        public List<TestTemplate> TemplateControls;
        public List<Test> TestList;

        Dictionary<ETestCommon, Label> dicCommonTestLabel;

        public frmRapidRead()
        {
            InitializeComponent();

            _currentDirection = VERTICAL;

            btnNext.TabStop = false;
            btnNext.FlatStyle = FlatStyle.Flat;
            btnNext.FlatAppearance.BorderSize = 0;
            btnNext.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent

            btnRotate.TabStop = false;
            btnRotate.FlatStyle = FlatStyle.Flat;
            btnRotate.FlatAppearance.BorderSize = 0;
            btnRotate.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent


            if (_currentDirection == HORIZONTAL)
                this.btnRotate.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.btn_rotate_horizontal));



            #region Init vertical controls

            #region control1
            control1_C.Init(TestResults.DicResults[ETest.ControlC1], EDirection.Vertical);
            control_BAR.Init(TestResults.DicResults[ETest.BAR], EDirection.Vertical);
            control_BZO.Init(TestResults.DicResults[ETest.BZO], EDirection.Vertical);
            #endregion

            #region control2
            control2_C.Init(TestResults.DicResults[ETest.ControlC2], EDirection.Vertical);
            control_THC.Init(TestResults.DicResults[ETest.THC], EDirection.Vertical);
            control_COC.Init(TestResults.DicResults[ETest.COC], EDirection.Vertical);
            #endregion

            #region control3
            control3_C.Init(TestResults.DicResults[ETest.ControlC3], EDirection.Vertical);
            control_OPI.Init(TestResults.DicResults[ETest.OPI], EDirection.Vertical);
            control_MET.Init(TestResults.DicResults[ETest.MET], EDirection.Vertical);
            #endregion

            #region control4
            control4_C.Init(TestResults.DicResults[ETest.ControlC4], EDirection.Vertical);
            control_TCA.Init(TestResults.DicResults[ETest.TCA], EDirection.Vertical);
            control_MTD.Init(TestResults.DicResults[ETest.MTD], EDirection.Vertical);
            #endregion

            #region control5
            control5_C.Init(TestResults.DicResults[ETest.ControlC5], EDirection.Vertical);
            control_MDMA.Init(TestResults.DicResults[ETest.MDMA], EDirection.Vertical);
            control_OXY.Init(TestResults.DicResults[ETest.OXY], EDirection.Vertical);
            #endregion

            #region control6
            control6_C.Init(TestResults.DicResults[ETest.ControlC6], EDirection.Vertical);
            control_AMP.Init(TestResults.DicResults[ETest.AMP], EDirection.Vertical);
            control_PCP.Init(TestResults.DicResults[ETest.PCP], EDirection.Vertical);
            #endregion

            #endregion

            #region Init Horizintal controls

            #region control1
            hControl1_C.Init(TestResults.DicResults[ETest.ControlC1], EDirection.Horizontal);
            hControl_BAR.Init(TestResults.DicResults[ETest.BAR], EDirection.Horizontal);
            hControl_BZO.Init(TestResults.DicResults[ETest.BZO], EDirection.Horizontal);
            #endregion

            #region control2
            hControl2_C.Init(TestResults.DicResults[ETest.ControlC2], EDirection.Horizontal);
            hControl_THC.Init(TestResults.DicResults[ETest.THC], EDirection.Horizontal);
            hControl_COC.Init(TestResults.DicResults[ETest.COC], EDirection.Horizontal);
            #endregion

            #region control3
            hControl3_C.Init(TestResults.DicResults[ETest.ControlC3], EDirection.Horizontal);
            hControl_OPI.Init(TestResults.DicResults[ETest.OPI], EDirection.Horizontal);
            hControl_MET.Init(TestResults.DicResults[ETest.MET], EDirection.Horizontal);
            #endregion

            #region control4
            hControl4_C.Init(TestResults.DicResults[ETest.ControlC4], EDirection.Horizontal);
            hControl_TCA.Init(TestResults.DicResults[ETest.TCA], EDirection.Horizontal);
            hControl_MTD.Init(TestResults.DicResults[ETest.MTD], EDirection.Horizontal);
            #endregion

            #region control5
            hControl5_C.Init(TestResults.DicResults[ETest.ControlC5], EDirection.Horizontal);
            hControl_MDMA.Init(TestResults.DicResults[ETest.MDMA], EDirection.Horizontal);
            hControl_OXY.Init(TestResults.DicResults[ETest.OXY], EDirection.Horizontal);
            #endregion

            #region control6
            hControl6_C.Init(TestResults.DicResults[ETest.ControlC6], EDirection.Horizontal);
            hControl_AMP.Init(TestResults.DicResults[ETest.AMP], EDirection.Horizontal);
            hControl_PCP.Init(TestResults.DicResults[ETest.PCP], EDirection.Horizontal);
            #endregion

            #endregion

            #region list template test

            TemplateControls = new List<TestTemplate>() 
            {
                // -------------vertical-----------------
                control1_C, control_BAR, control_BZO,
                control2_C, control_THC, control_COC,

                control3_C, control_OPI, control_MET,
                control4_C, control_TCA, control_MTD,

                control5_C, control_MDMA, control_OXY,
                control6_C, control_AMP, control_PCP,

                // -------------horizontal-----------------
                hControl1_C, hControl_BAR, hControl_BZO,
                hControl2_C, hControl_THC, hControl_COC,

                hControl3_C, hControl_OPI, hControl_MET,
                hControl4_C, hControl_TCA, hControl_MTD,

                hControl5_C, hControl_MDMA, hControl_OXY,
                hControl6_C, hControl_AMP, hControl_PCP
            };

            #endregion

            #region Init Common Test
            common_Cr.Init(TestResults.DicCommonResults[ETestCommon.Cr]);
            common_Ni.Init(TestResults.DicCommonResults[ETestCommon.Ni]);
            common_pH.Init(TestResults.DicCommonResults[ETestCommon.pH]);
            common_Bl.Init(TestResults.DicCommonResults[ETestCommon.Bl]);
            common_SG.Init(TestResults.DicCommonResults[ETestCommon.SG]);
            #endregion

            #region Init list Template common test 
            var commonTestControls = new List<TestCommonTemplate>()
            {
                common_Cr,
                common_Ni,
                common_pH,
                common_Bl,
                common_SG
            };
            #endregion

            #region labels for common test
            dicCommonTestLabel = new Dictionary<ETestCommon, Label>()
            {
                {ETestCommon.Cr, lbl_Cr},
                {ETestCommon.Ni, lbl_Ni},
                {ETestCommon.pH, lbl_pH},
                {ETestCommon.Bl, lbl_Bl},
                {ETestCommon.SG, lbl_SG}
            };
            #endregion
        }

        private void btnRotate_Click(object sender, EventArgs e)
        {
            if (_currentDirection == VERTICAL)
            {


                panelHorizontal.Visible = true;
                panelVertical.Visible = false;

                panelHorizontal.Location = new Point(219, 126);

                this.btnRotate.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.btn_rotate_vertical));
                _currentDirection = HORIZONTAL;
            }
            else
            {
                panelHorizontal.Visible = false;
                panelVertical.Visible = true;

                this.btnRotate.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.btn_rotate_horizontal));
                _currentDirection = VERTICAL;
            }
        }

        public void testControl_Click(object sender, EventArgs e)
        {
            TestTemplate control = sender as TestTemplate;

            // change observation
            control.test.ChangeObservationWithRules();

            // change background for horizontal and vertical control
            control.SetBackground(true);
            TemplateControls.Single(c => c.test.testType == control.test.testType
                && c.myDirection != control.myDirection).SetBackground(false);

            // update textbox display
            txtDisplay.Text = control.test.GetDisplay();
        }

        private void control_MouseHover(object sender, EventArgs e)
        {
            TestTemplate control = sender as TestTemplate;
            txtDisplay.Text = control.test.GetDisplay();
        }

        private void HighLightLabel(Label label)
        {
            label.BackColor = Color.FromArgb(217, 217, 217);
        }

        private void unHighLightLabel(Label label)
        {
            label.BackColor = Color.FromArgb(255, 255, 255);
        }

        private void ResetTextBoxDisplay()
        {
            txtDisplay.ResetText();
        }

        private void commonTestControl_Click(object sender, EventArgs e)
        {
            TestCommonTemplate commonTestSender = sender as TestCommonTemplate;

            commonTestSender.test.ChangeObservationWithRules();

            // change background for control just clicked
            commonTestSender.SetBackground();

            // update display
            txtDisplay.Text = commonTestSender.test.GetDisplay();

            // highlight label
            HighLightLabel(this.dicCommonTestLabel[commonTestSender.test.testType]);
        }

        private void colorBox_MouseLeave(object sender, EventArgs e)
        {
            TestCommonTemplate commonTestSender = sender as TestCommonTemplate;

            // reset textbox display
            ResetTextBoxDisplay();

            // unHighLight label
            unHighLightLabel(this.dicCommonTestLabel[commonTestSender.test.testType]);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmShowResults _frmShowResults = new frmShowResults();
            _frmShowResults.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmShowResults _frmShowResults = new frmShowResults();
            _frmShowResults.Show();
        }

        private void common_Test_MouseHover(object sender, EventArgs e)
        {
            TestCommonTemplate commonTestSender = sender as TestCommonTemplate;
            txtDisplay.Text = commonTestSender.test.GetDisplay();
        }
    }
}
